package fjut.acm.coding.controller;

import cn.hutool.core.util.IdUtil;
import fjut.acm.coding.entity.result.ResultCode;
import fjut.acm.coding.entity.result.ResultJson;
import fjut.acm.coding.utils.PictureUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Objects;

@RestController
@CrossOrigin
@RequestMapping("/upload")
public class UploadController {
    @Value("${cf.config.file.globalPath}")
    private String globalPath;

    @Value("${cf.config.file.picPath}")
    private String picPath;

    @Value("${cf.config.file.avatarPath}")
    private String avatarPath;

    @RequestMapping("/avatar")
    public ResultJson uploadAvatar(@RequestBody final MultipartFile avatar)
            throws IOException {

        String filePath = PictureUtils.pictureAddress(avatar,avatarPath,"avatar-","/image/avatar/");
        if (Objects.isNull(filePath)) {
            return new ResultJson(ResultCode.BUSINESS_FAIL, "文件为空！");
        }
        return new ResultJson(ResultCode.REQUIRED_SUCCESS, "上传成功！", filePath);

    }


    @RequestMapping("/picture")
    public ResultJson uploadPicture(@RequestBody final MultipartFile picture)
            throws IOException {
        String filePath = PictureUtils.pictureAddress(picture,picPath,"picture-","/image/pic/");
        if (Objects.isNull(filePath)) {
            return new ResultJson(ResultCode.BUSINESS_FAIL, "文件为空！");
        }
        return new ResultJson(ResultCode.REQUIRED_SUCCESS, "上传成功！", filePath);
    }


}
